<template>
  <div class="ShipmentList">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了~"
      @load="onLoad"
    >
      <div v-if="list.length > 0">
        <ShipmentListItem
          v-for="(item, index) in list"
          :key="index"
          :data="{ ...item, index: index }"
        />
      </div>
      <van-empty v-else />
    </van-list>
  </div>
</template>

<script>
import Vue from "vue";
import { List, Empty } from "vant";
import ShipmentListItem from "@/components/ShipmentListItem";
import { getShipmentList } from "./../services/api";
Vue.use(List);
Vue.use(Empty);
export default {
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      params: {
        pageNum: 1,
        pageSize: 20,
      },
    };
  },
  mounted() {
    window["reload"] = () => {
      this.list = [];
      this.loading = false;
      this.finished = false;
      this.params = {
        pageNum: 1,
        pageSize: 20,
      };
    };
  },
  methods: {
    onLoad() {
      getShipmentList(this.params).then((res) => {
        if (res) {
          // 加载状态结束
          this.loading = false;
          this.list = this.list.concat(res.list);
          this.params.pageNum += 1;
          if (res.list.length < 20) {
            this.finished = true;
          }
        }
      });
    },
  },
  components: {
    ShipmentListItem,
  },
};
</script>

<style lang="scss" scoped>
.ShipmentList {
  width: 100%;
  padding: 10px;
  box-sizing: border-box;
}
</style>